package com.duan.laugh.email.listener;

import com.duan.laugh.email.api.constans.EmailConstants;
import com.duan.laugh.email.api.pojo.entity.MailRequest;
import com.duan.laugh.email.service.MailService;
import com.duan.laugh.msg.api.pojo.entity.MsgSend;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 队列消息监听
 *
 * @author duanjw
 */
@Component
@Slf4j
@AllArgsConstructor
public class EmailReceiveListener {

    private final MailService mailService;

    /**
     * 监听email队列消息
     *
     * @param msgSend
     */
    @RabbitListener(queues = EmailConstants.MQ_QUEUE_EMAIL)
    public void receive(MsgSend msgSend) {
        // 发送邮件
        mailService.sendMailAndLogTime(new MailRequest()
                .setRec(msgSend.getReceiveObjectValue())
                .setParam(msgSend.getParams())
                .setTenantId(String.valueOf(msgSend.getTenantId()))
                .setTemplateCode(String.valueOf(msgSend.getTemplateId())));
        log.info("给指定用户推送邮件消息完毕");
    }
}
